﻿namespace System
{
    public static class ExceptionExtensions
    {
        public static string ToString(this Exception exception)
        {
            string result = "";
            int innerLevel = 0;
            while(exception != null)
            {
                result += string.Format("{{{3}{0}: {1}\r\n{2}}}\n", 
                    exception.GetType().Name, 
                    exception.Message, 
                    exception.StackTrace, 
                    (innerLevel > 0 ? "InnerExceptionLevel#" + innerLevel + ": " : ""));
                innerLevel++;
            }
            return result;
        }
    }
}